﻿using System;
using System.Data;
using System.Data.SqlClient;
using FunctionalCLR.Core;

namespace FunctionalCLR.Shell.Databasing
{
    public class CloseDatabaseNode : FuncNode
    {
        public CloseDatabaseNode()
            : base("closeDatabase")
        {
        }

        public override object Evaluate()
        {
            int length = Parameters.Count;
            if (length < 1)
            {
                throw new ArgumentException(
                    "");
            }

            SqlConnection conn = Parameters[length - 1].Evaluate() as SqlConnection;
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

            return string.Empty;
        }
    }
}
